package com.ziyia.vierpager2;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private ViewPager2 viewPager2;
    private View selected;
    private TextView one, two, three, five;


    private void textViewInit() {
        one = findViewById(R.id.one);
        two = findViewById(R.id.two);
        three = findViewById(R.id.three);
        five = findViewById(R.id.five);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textViewInit();

        // 创建一个容器来存储我们每个fragment布局
        List<Fragment> fragments = new ArrayList<>();
        fragments.add(FragmentOne.newInstance("首页"));
        fragments.add(FragmentOne.newInstance("通讯录"));
        fragments.add(FragmentOne.newInstance("发现"));
        fragments.add(FragmentOne.newInstance("我"));

        // 获取ViewPager2布局
        viewPager2 = findViewById(R.id.viewpager2);
        // 设置布局适配器
        viewPager2.setAdapter(new MyAdapter(getSupportFragmentManager(), getLifecycle(), fragments));

        viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                setPosition(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);
            }
        });
    }
    private void setPosition(int position) {
        if (selected != null) {
            selected.setSelected(false);
        }
        TextView textView = null;
        switch (position) {
            case R.id.one:
                viewPager2.setCurrentItem(0);
            case 0:
                textView = one;
                break;
            case R.id.two:
                viewPager2.setCurrentItem(1);
            case 1:
                textView = two;
                break;
            case R.id.three:
                viewPager2.setCurrentItem(2);
            case 2:
                textView = three;
                break;
            case R.id.five:
                viewPager2.setCurrentItem(3);
            case 3:
                textView = five;
                break;

        }
        textView.setSelected(true);
        selected = textView;
    }

    public void textViewClick(View view) {
        setPosition(view.getId());
    }



}